package com.example.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.beardedhen.androidbootstrap.TypefaceProvider;
import com.example.myapplication.data.MessageEvent;
import com.example.myapplication.databinding.ActivityMainBinding;

import com.example.myapplication.frags.CommonService_Fragment;

import com.example.myapplication.frags.Notification_Fragment;
import com.example.myapplication.frags.SchoolMap_Fragment;
import com.example.myapplication.frags.UserBasic_Fragment;

import android.widget.ImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity  {

    private ActivityMainBinding binding;
    private String TAG = "INFO";
    FragmentManager supportFragmentManager = getSupportFragmentManager();


    Fragment me = UserBasic_Fragment.newInstance("a","1");
    Fragment news = Notification_Fragment.newInstance();
    Fragment map = SchoolMap_Fragment.newInstance("a","1");
    Fragment common = CommonService_Fragment.newInstance("a","1");
    Fragment curFrag;


    ImageView lastBtn ;
    int lastBtnOriImg;


    /**
     * 为底部的导航按钮（其实是个图片）添加事件
     * @param targetObj 按钮（图片）
     * @param bindFrag 要跳转的Fragment
     * @param clickImage 点击时显示的图片
     * @param oriImage 本来应该是什么图片
     * @param debugName 调试信息
     */
    private void setSwitchBtnClickListeners(ImageView targetObj,Fragment bindFrag,int clickImage,int oriImage,String debugName){
        targetObj.setOnClickListener(null);
        targetObj.setOnClickListener((view)->{
            if(curFrag != bindFrag){
                targetObj.setImageResource(clickImage);

                Log.e(TAG, "onCreate: != "+debugName);
                FragmentTransaction ft = supportFragmentManager.beginTransaction();
                if(!bindFrag.isAdded()){
                    ft.hide(curFrag).add(R.id.frag_container,bindFrag).commit();
                    Log.e(TAG, "onCreate: "+debugName+"not added");
                }else{
                    ft.hide(curFrag).show(bindFrag).commit();
                    Log.e(TAG, "onCreate: "+debugName+"show");
                }

                //还原图片内容
                lastBtn.setImageResource(lastBtnOriImg);

                curFrag = bindFrag;

                lastBtn = targetObj;
                lastBtnOriImg = oriImage;
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceProvider.registerDefaultIconSets();
        EventBus.getDefault().register(this);


        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frag_container,news).commit();
        lastBtn = binding.btnNews;
        lastBtnOriImg = R.mipmap.home;
        binding.btnNews.setImageResource(R.mipmap.colored_home);

        curFrag = news;
        // 申请单个权限
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_BACKGROUND_LOCATION,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.INTERNET
        }, 1);


        setSwitchBtnClickListeners(binding.btnNews,news,R.mipmap.colored_home,R.mipmap.home,"首页");
        setSwitchBtnClickListeners(binding.btnMap,map,R.mipmap.colored_map,R.mipmap.map,"地图");
        setSwitchBtnClickListeners(binding.btnMisc,common,R.mipmap.colered_misc,R.mipmap.misc,"办事");
        setSwitchBtnClickListeners(binding.btnMe,me,R.mipmap.colered_me,R.mipmap.me,"我的");

        //注册一个总线。让所有的fragment都监听登录事件！！！
        EventBus.getDefault().register(me);
        EventBus.getDefault().register(map);
        EventBus.getDefault().register(news);
        EventBus.getDefault().register(common);

    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void onMessageEvent(MessageEvent event) {
        Log.d("MAIN", "onMessageEvent: "+event);
        Log.d(TAG, "onMessageEvent: 执行完了！");
    }

}